<?php

namespace App\Module\Api\Controllers;
use App\Module\Api\Controllers\Request\GoodsDetailsRequest;
use App\Module\Api\Validator\GoodsDetailsValidator;
use App\System\Goods\Repositories\GoodsAlbumRepository;
use App\System\Goods\Repositories\GoodsInfoRepository;

/**
 * 产品详细
 * @package App\Module\Admin\Controllers
 */
class GoodsDetailsController extends Controller
{
    public function __construct()
    {
        $this->query = new GoodsDetailsRequest();
        parent::__construct();
    }

    public function index()
    {
        (new GoodsDetailsValidator())->check();
        $goods = GoodsInfoRepository::getInstance();
        $goods_info = $goods->getDetail($this->query->id);
        if(!$goods_info || $goods_info->status == 2)
        {
            throw new ParamsException([
                'error_code' => '1011',
                'message'    => '产品不存在，或已下架',
            ]);
        }
        $goods_album = GoodsAlbumRepository::getInstance();
        $goods_album = $goods_album->getlist($this->query->id,true);
        $image = [];
        foreach ($goods_album as $v)
        {
            $image[] = $v['image']['path'].$v['image']['filename'];
        }
        $data = [
            'id' => $goods_info->id,
            'name' => $goods_info->name,
            'salesVolume' => $goods_info->sales_volume,
            'status'=>$goods_info->status,
            'description' => $goods_info->decription,
            'images' =>$image,
            'price'=> $goods_info->member_price,
            'originalPrice' => $goods_info->market_price,
            'content' => $goods_info->content
        ];

        return apiResponse(['goods'=>$data]);
    }
}
